package com.sparrow.common.map.vector;

/**
 * 二维向量
 */
public class Vector2 implements Cloneable {
    public float x, y;

    // 构造函数
    public Vector2() { this(0,0); }
    public Vector2(float x, float y) { this.x = x; this.y = y; }
    public Vector2(Vector2 other) { this.x = other.x; this.y = other.y; }

    // ================= 基础运算 =================
    public Vector2 add(Vector2 v) {
        return new Vector2(this.x + v.x, this.y + v.y);
    }

    public Vector2 subtract(Vector2 v) {
        return new Vector2(this.x - v.x, this.y - v.y);
    }

    public Vector2 multiply(float scalar) {
        return new Vector2(this.x * scalar, this.y * scalar);
    }

    public Vector2 divide(float scalar) {
        if (scalar == 0) throw new ArithmeticException("Divide by zero");
        return new Vector2(this.x / scalar, this.y / scalar);
    }

    public Vector2 negate() {
        return new Vector2(-x, -y);
    }

    // ================= 向量属性 =================
    public float length() {
        return (float) Math.sqrt(x*x + y*y);
    }

    public float lengthSquared() {
        return x*x + y*y;
    }

    public Vector2 normalize() {
        float len = length();
        if (len == 0) return new Vector2(0,0);
        return divide(len);
    }

    public float distance(Vector2 v) {
        return subtract(v).length();
    }

    public float distanceSquared(Vector2 v) {
        return subtract(v).lengthSquared();
    }

    // ================= 点积 =================
    public float dot(Vector2 v) {
        return x*v.x + y*v.y;
    }

    // ================= 旋转 =================
    public Vector2 rotate(float radians) {
        float cos = (float) Math.cos(radians);
        float sin = (float) Math.sin(radians);
        float nx = x * cos - y * sin;
        float ny = x * sin + y * cos;
        return new Vector2(nx, ny);
    }

    // ================= 插值 =================
    public Vector2 lerp(Vector2 target, float t) {
        return this.multiply(1 - t).add(target.multiply(t));
    }

    // ================= 角度 =================
    public float angleBetween(Vector2 v) {
        float lenProduct = this.length() * v.length();
        if (lenProduct == 0) return 0;
        float dot = this.dot(v) / lenProduct;
        dot = Math.max(-1f, Math.min(1f, dot)); // Clamp
        return (float) Math.acos(dot); // 弧度
    }

    public float angle() {
        return (float) Math.atan2(y, x); // 当前向量角度，相对于 X 轴
    }

    // ================= 克隆 / 输出 =================
    @Override
    public Vector2 clone() {
        return new Vector2(this);
    }

    @Override
    public String toString() {
        return String.format("Vector2(%.3f, %.3f)", x, y);
    }

    // ================= 静态常用向量 =================
    public static final Vector2 ZERO = new Vector2(0,0);
    public static final Vector2 ONE = new Vector2(1,1);
    public static final Vector2 UP = new Vector2(0,1);
    public static final Vector2 DOWN = new Vector2(0,-1);
    public static final Vector2 LEFT = new Vector2(-1,0);
    public static final Vector2 RIGHT = new Vector2(1,0);
}

